package sample.ui


import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PixelFormat
import android.os.Build
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import kotlinx.coroutines.script.C
import kotlinx.coroutines.script.GComponents
import kotlinx.coroutines.script.tes.dp
import kotlinx.coroutines.script.tes.phoneScreenHeight
import kotlinx.coroutines.script.tes.phoneScreenWidth
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.pt
import kotlinx.coroutines.script.tes.runOnUIThread


class RectLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : View(context, attrs) {
    private val windowManager by lazy { context.getSystemService(Context.WINDOW_SERVICE) as WindowManager }
    private val windowParams  by lazy {
        WindowManager.LayoutParams().also {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                it.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            } else {
                it.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
            }
            it.format = PixelFormat.RGBA_8888
            it.flags = (WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                    or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    or WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL) // 不拦截事件. 可能部分手机无效
            it.gravity = Gravity.START or Gravity.BOTTOM
            it.x = 0
            it.y = 0
            it.width = phoneScreenWidth
            it.height = phoneScreenHeight
        }
    }

    private val paint by lazy { Paint(Paint.ANTI_ALIAS_FLAG).apply { color = 0x88ffffff.toInt() } }
    private val textPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG).apply {
            color = 0xAAFFFF00.toInt()
            textSize = 8f.dp
        }
    }


    init {
        windowManager.addView(this, windowParams)
    }

    fun removeFromWindow() {
        if (isAttachedToWindow) windowManager.removeView(this)
    }

    private var displayLine = false
    private val displayMapLine = false
//    private val map get() = GComponents.get().cMap.map


    fun displayLine() {
//        if (GComponents.get().cMap.map != GMap.NONE && Properties.area.isNotEmpty()) {
//            displayLine = false
//            displayMapLine = true
//        } else {
//            displayLine = true
//            displayMapLine = false
//        }
        displayLine = true

        postInvalidate()
    }

    fun displayByInitCompleted() {
        displayLine = false
       // displayMapLine = false
        postInvalidate()
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        innerDraw(canvas)
    }


    private fun innerDraw(canvas: Canvas) {
        if (displayLine) {
            textPaint.color = 0xAAFFFF00.toInt()
            var drawX = 0f
            var drawXValue = 0
            while (drawX < width) {
                paint.color = 0x88ffffff.toInt()
                canvas.drawLine(drawX, 0f, drawX, height.toFloat(), paint)
                canvas.drawText(drawXValue.toString(), drawX, 200f.pt, textPaint)
                repeat(4) {
                    paint.color = 0x33ffffff
                    val subLineX = drawX + (it + 1) * 20.pt
                    canvas.drawLine(subLineX, 0f, subLineX, height.toFloat(), paint)
                }
                drawX += 100.pt // 竖线
                drawXValue += 100
            }
            var drawY = 0f
            var drawYValue = 0
            while (drawY < height) {
                paint.color = 0x88ffffff.toInt()
                canvas.drawLine(0f, drawY, width.toFloat(), drawY, paint)
                canvas.drawText(drawYValue.toString(), 200f.pt, drawY, textPaint)
                repeat(4) {
                    paint.color = 0x33ffffff
                    val subLineY = drawY + (it + 1) * 20.pt
                    canvas.drawLine(0f, subLineY, width.toFloat(), subLineY, paint)
                }
                drawY += 100.pt // 横线
                drawYValue += 100
            }
        }
//        if (displayMapLine && map != GMap.NONE) {
//            textPaint.color = 0xAAFFFF00.toInt()
//            val cell = map.height / 10f
//            var drawY = phoneScreenHeight / 2f
//            var drawYValue = 0
//            while (drawY > map.top) {
//                paint.color = 0x88ffffff.toInt()
//                canvas.drawLine(map.left.toFloat(), drawY, map.right.toFloat(), drawY, paint)
//                canvas.drawText(drawYValue.toString(), 200f.pt, drawY, textPaint)
//                paint.color = 0x33ffffff
//                canvas.drawLine(map.left.toFloat(), drawY + cell / 2, map.right.toFloat(), drawY + cell / 2, paint)
//                drawY -= cell // 横线
//                drawYValue -= 100
//            }
//            drawY = phoneScreenHeight / 2f
//            drawYValue = 0
//            while (drawY < map.bottom) {
//                paint.color = 0x88ffffff.toInt()
//                canvas.drawLine(map.left.toFloat(), drawY, map.right.toFloat(), drawY, paint)
//                canvas.drawText(drawYValue.toString(), 200f.pt, drawY, textPaint)
//                paint.color = 0x33ffffff
//                canvas.drawLine(map.left.toFloat(), drawY + cell / 2, map.right.toFloat(), drawY + cell / 2, paint)
//                drawY += cell // 横线
//                drawYValue += 100
//            }
//
//            var drawX = phoneScreenWidth / 2f
//            var drawXValue = 0
//            while (drawX < map.right) { // 10.pt容差
//                paint.color = 0x88ffffff.toInt()
//                canvas.drawLine(drawX, map.top.toFloat(), drawX, map.bottom.toFloat(), paint)
//                canvas.drawText(drawXValue.toString(), drawX, 200f.pt, textPaint)
//                paint.color = 0x33ffffff
//                canvas.drawLine(drawX + cell / 2, map.top.toFloat(), drawX + cell / 2, map.bottom.toFloat(), paint)
//                drawX += cell // 竖线
//                drawXValue += 100
//            }
//            drawX = phoneScreenWidth / 2f
//            drawXValue = 0
//            while (drawX > map.left) { // 10.pt容差
//                paint.color = 0x88ffffff.toInt()
//                canvas.drawLine(drawX, map.top.toFloat(), drawX, map.bottom.toFloat(), paint)
//                canvas.drawText(drawXValue.toString(), drawX, 200f.pt, textPaint)
//                paint.color = 0x33ffffff
//                canvas.drawLine(drawX + cell / 2, map.top.toFloat(), drawX + cell / 2, map.bottom.toFloat(), paint)
//                drawX -= cell // 竖线
//                drawXValue -= 100
//            }
//        }
        //

        if (GComponents.isInitialized) {
            if (Properties.area.isEmpty()) {
                GComponents.get().widgetsWithoutMap.forEach {
                    paint.color = if (it.name == C.V_CENTER_LINE) Color.RED else 0x88ffffff.toInt()
                    if (it.name == C.V_MEDICINE ||
                        it.name == C.V_INTERACT ||
                        it.name == C.V_ROCKER ||
                        it.name == C.V_ATK ||
                        it.name == C.V_SKILL_A ||
                        it.name == C.V_SKILL_B ||
                        it.name == C.V_SKILL_C ||
                        it.name == C.V_SKILL_D ||
                        it.name == C.V_SKILL_E ||
                        it.name == C.V_SKILL_F_800 ||
                        it.name == C.V_SKILL_F_800_B ) {
                        canvas.drawCircle(it.x.toFloat(), it.y.toFloat(), it.width / 2f, paint)
                        canvas.drawText(it.name, it.left.toFloat(), it.y.toFloat() - 10.pt, textPaint)
                    } else {
                        canvas.drawRect(it.left.toFloat(), it.top.toFloat(), it.left.toFloat() + it.width, it.top.toFloat() + it.height, paint)
                        canvas.drawText(it.name, it.left.toFloat(), it.top.toFloat() + 20.pt, textPaint)
                    }
                }
            } else {
                // 打野 内置路线
                GComponents.get().widgetsInMap.forEach {
                    paint.color = if (it.name == C.V_CENTER_LINE) Color.RED else 0x88ffffff.toInt()
                    if (it.name == C.V_MAP_BACK ||
                        it.name == C.V_MAP_GLASS) {
                        canvas.drawCircle(it.x.toFloat(), it.y.toFloat(), it.width / 2f, paint)
                        canvas.drawText(it.name, it.left.toFloat(), it.y.toFloat() - 10.pt, textPaint)
                    } else {
                        canvas.drawRect(it.left.toFloat(), it.top.toFloat(), it.left.toFloat() + it.width, it.top.toFloat() + it.height, paint)
                        canvas.drawText(it.name, it.left.toFloat(), it.top.toFloat() + 20.pt, textPaint)
                    }
                }

//                if (!FarmingAdjustLayout.displaying) {
//                    Properties.selectedChildAreaList.forEachIndexed { idx, ca ->
//                        val mapPoints = GComponents.get().cMap.points.sweepingPlaceDotList("${Properties.area}_${ca}")
//                        mapPoints.forEachIndexed { index, it ->
//                            val pointColor = when (idx) { // 一般只有一组打野点
//                                1 -> 0x66ff0000
//                                2 -> 0x6600ff00
//                                else -> 0x880000ff.toInt()
//                            }
//                            paint.color = pointColor
//                            textPaint.color = pointColor
//                            canvas.drawCircle(it.block.x.toFloat(), it.block.y.toFloat(), 10f.pt, paint)
//                            canvas.drawText("${index + 1}[${if(it.keepTime <= 0) "" else it.keepTime}]", it.block.left.toFloat() + 10.pt, it.block.top.toFloat() + 20.pt, textPaint)
//                        }
//                    }
//                }
            }
        }

    }

    companion object {
        private var rectLayout: RectLayout? = null
        val displaying get() = rectLayout != null
        fun displayByInitCompleted(context: Context) = runOnUIThread {
            printLog { "RectLayout displayByInitCompleted" }
            if (rectLayout == null) RectLayout(context).also { rectLayout = it }
            rectLayout?.displayByInitCompleted()
        }
        fun displayLine(context: Context) {
            printLog { "RectLayout displayLine" }
            if (rectLayout == null) RectLayout(context).also { rectLayout = it }
            rectLayout?.displayLine()
        }




        fun hide() = runOnUIThread {
            printLog { "RectLayout hide" }
            rectLayout?.removeFromWindow().also { rectLayout = null }
        }
    }
}